package com.adingxiong.cft.thread;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName ExecutorThread
 * @Description  线程池的使用
 * @Author xiongchao
 * @Date 2020/12/6 15:53
 **/
public class ExecutorThread {
    private static ExecutorService exe = Executors.newFixedThreadPool(10);

    public static void moreThread() {
        try {
            int threadNum = 0;
            for (int i = 0; i < 10; i++) {
                threadNum++;

                final int currentThreadNum = threadNum;
                exe.execute( () -> {
                    try {
                        System.out.println("子线程[" + currentThreadNum + "]开启");
                        Thread.sleep(1000*10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally{
                        System.out.println("子线程[" + currentThreadNum + "]结束");
                    }
                });
            }

            System.out.println("已经开启所有的子线程");
            exe.shutdown();
            System.out.println("shutdown()：启动一次顺序关闭，执行以前提交的任务，但不接受新任务。");
            while(true){
                if(exe.isTerminated()){
                    System.out.println("所有的子线程都结束了！");
                    break;
                }
                Thread.sleep(1000);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            System.out.println("主线程结束");
        }
    }

    public static void main(String[] args) {
        moreThread();
    }
}

